package com.wpp.concurrent.demo6;

/**
 * @description: 优雅的关闭线程 优雅的
 * @author: WangPengPeng
 * @create: 2022-01-23 21:41
 **/
public class CloseThreadElegant {

    public static void main(String[] args) throws InterruptedException {
        MyThread myThread = new MyThread();
        myThread.start();
        Thread.sleep(5000);
        myThread.stopRunning();
        myThread.join();
    }
}

 class MyThread extends Thread {
    private boolean running = true;

    @Override
    public void run() {
        while (running) {
            System.out.println("线程正在运行。。。");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void stopRunning() {
        this.running = false;
    }
}